/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HolidayDialog.java
 *
 * Created on 26-May-2010, 23:19:01
 */

package TimeRecording.forms;

import TimeRecording.TimeRecordingView;
import TimeRecording.business.DaisyChainUtilities;
import TimeRecording.business.DateRange;
import TimeRecording.business.FilteredQuery;
import TimeRecording.business.HolidayCalculations;
import TimeRecording.business.SpendExplanation;
import TimeRecording.enums.HolidayRequestStatus;
import TimeRecording.reporting.CalendarReport;
import TimeRecording.reporting.HolidayIconProvider;
import TimeRecording.tables.Employee;
import TimeRecording.tables.HolidayRequest;
import TimeRecording.widgets.DaisyChainCalendar;
import TimeRecording.widgets.EmployeeComboBox;
import TimeRecording.widgets.HolidayStatusComboBox;
import java.awt.Cursor;
import java.beans.Beans;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.application.Action;
import org.jdesktop.observablecollections.ObservableCollections;

/**
 *
 * @author david
 */
public class HolidayRequestDialog extends javax.swing.JDialog {

    // Dropdown constants

    private final static int ALL_REQUESTS_ID = 0;
    private final static int THIS_YEARS_REQUESTS_ID = 1;
    private final static int PAST_REQUESTS_ID = 2;
    private final static int CURRENT_REQUESTS_ID = 3;
    private final static int FUTURE_REQUESTS_ID = 4;
    private final static int CUSTOM_REQUESTS_ID = 5;
    
    private TimeRecordingView myParent;

    private HolidayCalculations holidayCalculations = new HolidayCalculations();
    private SpendExplanation explanation = new SpendExplanation();

    // <editor-fold defaultstate="collapsed" desc="EntityManager">
    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        EntityManager oldEntityManager = this.entityManager;
        this.entityManager = entityManager;
        firePropertyChange("entityManager", entityManager, entityManager);
    } // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="editable">
    protected boolean editable = false;

    public boolean isEditable() {
	return  editable;
    }

    public void setEditable(boolean editable) {
        boolean oldEditable = this.editable;
        this.editable =  editable;
        firePropertyChange("editable", oldEditable, editable);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="editableByAdministrator">
    protected boolean editableByAdministrator = false;

    public boolean isEditableByAdministrator() {
	return  editableByAdministrator;
    }

    public void setEditableByAdministrator(boolean editableByAdministrator) {
        boolean oldEditableByAdministrator = this.editableByAdministrator;
        this.editableByAdministrator =  editableByAdministrator;
        firePropertyChange("editableByAdministrator", oldEditableByAdministrator, editableByAdministrator);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="approvable">
    protected boolean approvable = false;

    public boolean isApprovable() {
	return approvable;
    }

    public void setApprovable(boolean approvable) {
        boolean oldApprovable = this.approvable;
        this.approvable = approvable;
        firePropertyChange("approvable", oldApprovable, approvable);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="unapprovable">
    protected boolean unapprovable = false;

    public boolean isUnapprovable() {
	return unapprovable;
    }

    public void setUnapprovable(boolean unapprovable) {
        boolean oldUnapprovable = this.unapprovable;
        this.unapprovable = unapprovable;
        firePropertyChange("unapprovable", oldUnapprovable, unapprovable);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="retractable">
    protected boolean retractable = false;

    public boolean isRetractable() {
	return  retractable;
    }

    public void setRetractable(boolean retractable) {
        boolean oldRetractable = this.retractable;
        this.retractable =  retractable;
        firePropertyChange("retractable", oldRetractable, retractable);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="notYetSubmitted">
    protected boolean notYetSubmitted = false;

    public boolean isNotYetSubmitted() {
	return notYetSubmitted;
    }

    public void setNotYetSubmitted(boolean notYetSubmitted) {
        boolean oldNotYetSubmitted = this.notYetSubmitted;
        this.notYetSubmitted =  notYetSubmitted;
        firePropertyChange("notYetSubmitted", oldNotYetSubmitted, notYetSubmitted);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="recordSelected">
    protected boolean recordSelected = false;

    public boolean isRecordSelected() {
	return recordSelected;
    }

    public void setRecordSelected(boolean recordSelected) {

        boolean oldRecordSelected = this.recordSelected;
        this.recordSelected = recordSelected;
        firePropertyChange("recordSelected", oldRecordSelected, recordSelected);

        assessWhatShouldBeEnabled();
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="administratorLoggedIn">
    protected boolean administratorLoggedIn = false;

    public boolean isAdministratorLoggedIn() {
        return administratorLoggedIn;
    }

    public void setAdministratorLoggedIn(boolean administratorLoggedIn) {

        boolean oldAdministratorLoggedIn = this.administratorLoggedIn;
        this.administratorLoggedIn = administratorLoggedIn;
        firePropertyChange("administratorLoggedIn", oldAdministratorLoggedIn, administratorLoggedIn);

        assessWhatShouldBeEnabled();
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="employeeFilterable">
    protected boolean employeeFilterable = false;

    public boolean isEmployeeFilterable() {
        return employeeFilterable;
    }

    public void setEmployeeFilterable(boolean employeeFilterable) {
        boolean oldEmployeeFilterable = this.employeeFilterable;
        this.employeeFilterable = employeeFilterable;
        firePropertyChange("employeeFilterable", oldEmployeeFilterable, employeeFilterable);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="statusFilterable">
    protected boolean statusFilterable = false;

    public boolean isStatusFilterable() {
        return statusFilterable;
    }

    public void setStatusFilterable(boolean statusFilterable) {
        boolean oldStatusFilterable = this.statusFilterable;
        this.statusFilterable = statusFilterable;
        firePropertyChange("statusFilterable", oldStatusFilterable, statusFilterable);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="filteredEmployee">
    protected Employee filteredEmployee;

    public Employee getFilteredEmployee() {
        return filteredEmployee;
    }

    public void setFilteredEmployee(Employee filteredEmployee) {
        Employee oldFilteredEmployee = this.filteredEmployee;
        this.filteredEmployee = filteredEmployee;
        firePropertyChange("filteredEmployee", oldFilteredEmployee, filteredEmployee);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="filteredStartDate">
    protected Date filteredStartDate;

    public Date getFilteredStartDate() {
        return filteredStartDate;
    }

    public void setFilteredStartDate(Date filteredStartDate) {
        Date oldFilteredStartDate = this.filteredStartDate;
        this.filteredStartDate = filteredStartDate;
        firePropertyChange("filteredStartDate", oldFilteredStartDate, filteredStartDate);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="filteredEndDate">
    protected Date filteredEndDate;

    public Date getFilteredEndDate() {
        return filteredEndDate;
    }

    public void setFilteredEndDate(Date filteredEndDate) {
        Date oldFilteredEndDate = this.filteredEndDate;
        this.filteredEndDate = filteredEndDate;
        firePropertyChange("filteredEndDate", oldFilteredEndDate, filteredEndDate);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="filteredStatus">
    protected HolidayRequestStatus filteredStatus;

    public HolidayRequestStatus getFilteredStatus() {
        return filteredStatus;
    }

    public void setFilteredStatus(HolidayRequestStatus filteredStatus) {
        HolidayRequestStatus oldFilteredStatus = this.filteredStatus;
        this.filteredStatus = filteredStatus;
        firePropertyChange("filteredStatus", oldFilteredStatus, filteredStatus);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="employeeFilterChangeable">
    protected boolean employeeFilterChangeable;

    public boolean isEmployeeFilterChangeable() {
        return employeeFilterChangeable;
    }

    public void setEmployeeFilterChangeable(boolean employeeFilterChangeable) {
        boolean oldEmployeeFilterChangeable = this.employeeFilterChangeable;
        this.employeeFilterChangeable = employeeFilterChangeable;
        firePropertyChange("employeeFilterChangeable", oldEmployeeFilterChangeable, employeeFilterChangeable);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="dateComboBeingActioned">

    protected boolean dateComboBeingActioned = false;

    public boolean getDateComboBeingActioned() {
        return dateComboBeingActioned;
    }

    public void setDateComboBeingActioned(boolean dateComboBeingActioned) {
        boolean oldDateComboBeingActioned = this.dateComboBeingActioned;
        this.dateComboBeingActioned = dateComboBeingActioned;
        firePropertyChange("dateComboBeingActioned", oldDateComboBeingActioned, dateComboBeingActioned);
    } // </editor-fold>

    private void assessWhatShouldBeEnabled() {

        setApprovable(
                isRecordSelected() &&
                isAdministratorLoggedIn() &&
                status.getText().equals(HolidayRequestStatus.SUBMITTED.toString()));

        setRetractable(
                isRecordSelected() &&
                (status.getText().equals(HolidayRequestStatus.SUBMITTED.toString()) ||
                 status.getText().equals(HolidayRequestStatus.APPROVED.toString())));

        setNotYetSubmitted(
                isRecordSelected() &&
                (status.getText().equals(HolidayRequestStatus.PREPARATION.toString()) ||
                 status.getText().equals(HolidayRequestStatus.REJECTED.toString())));

        setUnapprovable(
                isRecordSelected() &&
                status.getText().equals(HolidayRequestStatus.APPROVED.toString()));

        setEditableByAdministrator(
                isNotYetSubmitted() &&
                isAdministratorLoggedIn());

        setEmployeeFilterChangeable(
                isAdministratorLoggedIn() && isEmployeeFilterable());
    }
    
    /**
     * Creates new form HolidayDialog
     */
    
    public HolidayRequestDialog(TimeRecordingView parent, boolean modal) {

        super(parent.getFrame(), modal);
        myParent = parent;
        initComponents();

        // tracking table selection
        requestTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent e) {
                        setRecordSelected(requestTable.getSelectedRow() != -1);
                    }
                });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        holidayRequestList = ObservableCollections.observableList(new java.util.LinkedList());
        holidayRequestStatusConverterBeanInfo1 = new TimeRecording.enums.HolidayRequestStatusConverterBeanInfo();
        holidayRequestStatusConverter = new TimeRecording.enums.HolidayRequestStatusConverter();
        dateConverter = new TimeRecording.widgets.DateConverter();
        partOfDayConverter = new TimeRecording.enums.PartOfDayConverter();
        entityManager = myParent.getEntityManager();
        filterPanel = new javax.swing.JPanel();
        employeeCheck = new javax.swing.JCheckBox();
        employeeFilterCombo = new EmployeeComboBox(entityManager);
        statusCheck = new javax.swing.JCheckBox();
        statusFilterCombo = new HolidayStatusComboBox();
        dateFilterLabel = new javax.swing.JLabel();
        dateFilterCombo = new javax.swing.JComboBox();
        dateFilterFromLabel = new javax.swing.JLabel();
        dateFilterFrom = new com.toedter.calendar.JDateChooser();
        dateFilterToLabel = new javax.swing.JLabel();
        dateFilterTo = new com.toedter.calendar.JDateChooser();
        filterButton = new javax.swing.JButton();
        tablePane = new javax.swing.JScrollPane();
        requestTable = new javax.swing.JTable();
        absenceSummaryPanel1 = new TimeRecording.forms.AbsenceSummaryPanel();
        detailPanel = new javax.swing.JPanel();
        employeeLabel = new javax.swing.JLabel();
        employee = new javax.swing.JTextField();
        administratorLabel = new javax.swing.JLabel();
        administrator = new javax.swing.JTextField();
        employeeCommentLabel = new javax.swing.JLabel();
        employeeComment = new javax.swing.JTextField();
        administratorCommentLabel = new javax.swing.JLabel();
        administratorComment = new javax.swing.JTextField();
        fromLabel = new javax.swing.JLabel();
        fromDate = new javax.swing.JTextField();
        firstDayPart = new javax.swing.JTextField();
        toLabel = new javax.swing.JLabel();
        toDate = new javax.swing.JTextField();
        lastDayPart = new javax.swing.JTextField();
        hoursSpentLabel = new javax.swing.JLabel();
        hoursSpent = new javax.swing.JFormattedTextField();
        explanationButton = new javax.swing.JButton();
        statusLabel = new javax.swing.JLabel();
        status = new javax.swing.JTextField();
        fromFlexiCheck = new javax.swing.JCheckBox();
        buttonPanel = new javax.swing.JPanel();
        newButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        submitButton = new javax.swing.JButton();
        retractButton = new javax.swing.JButton();
        approveButton = new javax.swing.JButton();
        rejectButton = new javax.swing.JButton();
        unapproveButton = new javax.swing.JButton();
        reportButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getResourceMap(HolidayRequestDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));

        filterPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEmptyBorder(8, 8, 4, 8), javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("filterPanel.border.insideBorder.title")))); // NOI18N
        filterPanel.setName("filterPanel"); // NOI18N
        filterPanel.setLayout(new java.awt.GridBagLayout());

        employeeCheck.setText(resourceMap.getString("employeeCheck.text")); // NOI18N
        employeeCheck.setName("employeeCheck"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${employeeFilterable}"), employeeCheck, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${administratorLoggedIn}"), employeeCheck, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 4, 0);
        filterPanel.add(employeeCheck, gridBagConstraints);

        employeeFilterCombo.setName("employeeFilterCombo"); // NOI18N
        employeeFilterCombo.setPreferredSize(new java.awt.Dimension(190, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${filteredEmployee}"), employeeFilterCombo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${employeeFilterable}"), employeeFilterCombo, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 50.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 4);
        filterPanel.add(employeeFilterCombo, gridBagConstraints);

        statusCheck.setText(resourceMap.getString("statusCheck.text")); // NOI18N
        statusCheck.setName("statusCheck"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${statusFilterable}"), statusCheck, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 8, 8, 0);
        filterPanel.add(statusCheck, gridBagConstraints);

        statusFilterCombo.setName("statusFilterCombo"); // NOI18N
        statusFilterCombo.setPreferredSize(new java.awt.Dimension(190, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${filteredStatus}"), statusFilterCombo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${statusFilterable}"), statusFilterCombo, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 50.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 8, 4);
        filterPanel.add(statusFilterCombo, gridBagConstraints);

        dateFilterLabel.setText(resourceMap.getString("dateFilterLabel.text")); // NOI18N
        dateFilterLabel.setName("dateFilterLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 4, 4);
        filterPanel.add(dateFilterLabel, gridBagConstraints);

        dateFilterCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "This Year", "Previous", "In Progress", "Future", "Custom" }));
        dateFilterCombo.setName("dateFilterCombo"); // NOI18N
        dateFilterCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                dateFilterComboItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 4, 4);
        filterPanel.add(dateFilterCombo, gridBagConstraints);

        dateFilterFromLabel.setText(resourceMap.getString("dateFilterFromLabel.text")); // NOI18N
        dateFilterFromLabel.setName("dateFilterFromLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 4, 4);
        filterPanel.add(dateFilterFromLabel, gridBagConstraints);

        dateFilterFrom.setMinimumSize(new java.awt.Dimension(150, 20));
        dateFilterFrom.setName("dateFilterFrom"); // NOI18N
        dateFilterFrom.setPreferredSize(new java.awt.Dimension(150, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${filteredStartDate}"), dateFilterFrom, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);

        dateFilterFrom.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dateFilterFromPropertyChange(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 50.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 4);
        filterPanel.add(dateFilterFrom, gridBagConstraints);

        dateFilterToLabel.setText(resourceMap.getString("dateFilterToLabel.text")); // NOI18N
        dateFilterToLabel.setName("dateFilterToLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 8, 8, 4);
        filterPanel.add(dateFilterToLabel, gridBagConstraints);

        dateFilterTo.setMinimumSize(new java.awt.Dimension(150, 20));
        dateFilterTo.setName("dateFilterTo"); // NOI18N
        dateFilterTo.setPreferredSize(new java.awt.Dimension(150, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${filteredEndDate}"), dateFilterTo, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);

        dateFilterTo.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dateFilterToPropertyChange(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 50.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 8, 4);
        filterPanel.add(dateFilterTo, gridBagConstraints);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getActionMap(HolidayRequestDialog.class, this);
        filterButton.setAction(actionMap.get("applyFilter")); // NOI18N
        filterButton.setText(resourceMap.getString("filterButton.text")); // NOI18N
        filterButton.setName("filterButton"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 8, 8, 8);
        filterPanel.add(filterButton, gridBagConstraints);

        getContentPane().add(filterPanel);

        tablePane.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEmptyBorder(4, 8, 4, 8), javax.swing.BorderFactory.createEtchedBorder()));
        tablePane.setName("tablePane"); // NOI18N

        requestTable.setAutoCreateRowSorter(true);
        requestTable.setName("requestTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, holidayRequestList, requestTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${employeeId}"));
        columnBinding.setColumnName("Employee Id");
        columnBinding.setColumnClass(TimeRecording.tables.Employee.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${firstDayOfHoliday}"));
        columnBinding.setColumnName("First Day Of Holiday");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${firstDayPart}"));
        columnBinding.setColumnName("First Day Part");
        columnBinding.setColumnClass(TimeRecording.enums.PartOfDay.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${lastDayOfHoliday}"));
        columnBinding.setColumnName("Last Day Of Holiday");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${lastDayPart}"));
        columnBinding.setColumnName("Last Day Part");
        columnBinding.setColumnClass(TimeRecording.enums.PartOfDay.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${status}"));
        columnBinding.setColumnName("Status");
        columnBinding.setColumnClass(TimeRecording.enums.HolidayRequestStatus.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${hoursSpent}"));
        columnBinding.setColumnName("Hours Spent");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${takenFromFlexi}"));
        columnBinding.setColumnName("Taken From Flexi");
        columnBinding.setColumnClass(Boolean.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        tablePane.setViewportView(requestTable);
        requestTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("holidayRequestTable.columnModel.title4")); // NOI18N
        requestTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("holidayRequestTable.columnModel.title0")); // NOI18N
        requestTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("requestTable.columnModel.title5")); // NOI18N
        requestTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("holidayRequestTable.columnModel.title1")); // NOI18N
        requestTable.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("requestTable.columnModel.title6")); // NOI18N
        requestTable.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("holidayRequestTable.columnModel.title2")); // NOI18N
        requestTable.getColumnModel().getColumn(6).setHeaderValue(resourceMap.getString("holidayRequestTable.columnModel.title3")); // NOI18N
        requestTable.getColumnModel().getColumn(7).setHeaderValue(resourceMap.getString("requestTable.columnModel.title7")); // NOI18N

        getContentPane().add(tablePane);

        absenceSummaryPanel1.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEmptyBorder(4, 8, 4, 8), javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("absenceSummaryPanel1.border.insideBorder.title")))); // NOI18N
        absenceSummaryPanel1.setName("absenceSummaryPanel1"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.employeeId.workingPatternsMaster}"), absenceSummaryPanel1, org.jdesktop.beansbinding.BeanProperty.create("workingPatternMaster"));
        bindingGroup.addBinding(binding);

        getContentPane().add(absenceSummaryPanel1);

        detailPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEmptyBorder(1, 8, 1, 8), javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("detailPanel.border.insideBorder.title")))); // NOI18N
        detailPanel.setName("detailPanel"); // NOI18N
        detailPanel.setLayout(new java.awt.GridBagLayout());

        employeeLabel.setText(resourceMap.getString("employeeLabel.text")); // NOI18N
        employeeLabel.setName("employeeLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 4, 4);
        detailPanel.add(employeeLabel, gridBagConstraints);

        employee.setEditable(false);
        employee.setName("employee"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.employeeId.friendlyName}"), employee, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceNullValue("");
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 25.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 4, 4);
        detailPanel.add(employee, gridBagConstraints);

        administratorLabel.setText(resourceMap.getString("administratorLabel.text")); // NOI18N
        administratorLabel.setName("administratorLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(administratorLabel, gridBagConstraints);

        administrator.setEditable(false);
        administrator.setName("administrator"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.administratorId.friendlyName}"), administrator, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceNullValue("");
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 25.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(administrator, gridBagConstraints);

        employeeCommentLabel.setText(resourceMap.getString("employeeCommentLabel.text")); // NOI18N
        employeeCommentLabel.setName("employeeCommentLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 4, 4);
        detailPanel.add(employeeCommentLabel, gridBagConstraints);

        employeeComment.setEditable(false);
        employeeComment.setName("employeeComment"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.employeeComment}"), employeeComment, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 75.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 4, 4);
        detailPanel.add(employeeComment, gridBagConstraints);

        administratorCommentLabel.setText(resourceMap.getString("administratorCommentLabel.text")); // NOI18N
        administratorCommentLabel.setName("administratorCommentLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 12, 4, 4);
        detailPanel.add(administratorCommentLabel, gridBagConstraints);

        administratorComment.setEditable(false);
        administratorComment.setName("administratorComment"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.administratorComment}"), administratorComment, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 75.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(administratorComment, gridBagConstraints);

        fromLabel.setText(resourceMap.getString("fromLabel.text")); // NOI18N
        fromLabel.setName("fromLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(fromLabel, gridBagConstraints);

        fromDate.setEditable(false);
        fromDate.setName("fromDate"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.firstDayOfHoliday}"), fromDate, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(dateConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 50.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(fromDate, gridBagConstraints);

        firstDayPart.setEditable(false);
        firstDayPart.setName("firstDayPart"); // NOI18N
        firstDayPart.setPreferredSize(new java.awt.Dimension(80, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.firstDayPart}"), firstDayPart, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(partOfDayConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(firstDayPart, gridBagConstraints);

        toLabel.setText(resourceMap.getString("toLabel.text")); // NOI18N
        toLabel.setName("toLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(toLabel, gridBagConstraints);

        toDate.setEditable(false);
        toDate.setName("toDate"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.lastDayOfHoliday}"), toDate, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(dateConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(toDate, gridBagConstraints);

        lastDayPart.setEditable(false);
        lastDayPart.setName("lastDayPart"); // NOI18N
        lastDayPart.setPreferredSize(new java.awt.Dimension(80, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.lastDayPart}"), lastDayPart, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(partOfDayConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(lastDayPart, gridBagConstraints);

        hoursSpentLabel.setText(resourceMap.getString("hoursSpentLabel.text")); // NOI18N
        hoursSpentLabel.setName("hoursSpentLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 12, 4, 4);
        detailPanel.add(hoursSpentLabel, gridBagConstraints);

        hoursSpent.setEditable(false);
        hoursSpent.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        hoursSpent.setName("hoursSpent"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.hoursSpent}"), hoursSpent, org.jdesktop.beansbinding.BeanProperty.create("value"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 50.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(hoursSpent, gridBagConstraints);

        explanationButton.setAction(actionMap.get("explainClicked")); // NOI18N
        explanationButton.setText(resourceMap.getString("explanationButton.text")); // NOI18N
        explanationButton.setName("explanationButton"); // NOI18N
        explanationButton.setPreferredSize(new java.awt.Dimension(130, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${recordSelected}"), explanationButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(explanationButton, gridBagConstraints);

        statusLabel.setText(resourceMap.getString("statusLabel.text")); // NOI18N
        statusLabel.setName("statusLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 12, 4, 4);
        detailPanel.add(statusLabel, gridBagConstraints);

        status.setEditable(false);
        status.setName("status"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.status}"), status, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(holidayRequestStatusConverter);
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 50.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(status, gridBagConstraints);

        fromFlexiCheck.setText(resourceMap.getString("fromFlexiCheck.text")); // NOI18N
        fromFlexiCheck.setEnabled(false);
        fromFlexiCheck.setName("fromFlexiCheck"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, requestTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.takenFromFlexi}"), fromFlexiCheck, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        detailPanel.add(fromFlexiCheck, gridBagConstraints);

        getContentPane().add(detailPanel);

        buttonPanel.setName("buttonPanel"); // NOI18N

        newButton.setAction(actionMap.get("newHoliday")); // NOI18N
        newButton.setText(resourceMap.getString("newButton.text")); // NOI18N
        newButton.setName("newButton"); // NOI18N
        buttonPanel.add(newButton);

        editButton.setAction(actionMap.get("editHoliday")); // NOI18N
        editButton.setText(resourceMap.getString("editButton.text")); // NOI18N
        editButton.setName("editButton"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${recordSelected}"), editButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        buttonPanel.add(editButton);

        deleteButton.setAction(actionMap.get("deleteHoliday")); // NOI18N
        deleteButton.setText(resourceMap.getString("deleteButton.text")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${notYetSubmitted}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        buttonPanel.add(deleteButton);

        submitButton.setAction(actionMap.get("submitHoliday")); // NOI18N
        submitButton.setText(resourceMap.getString("submitButton.text")); // NOI18N
        submitButton.setName("submitButton"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${notYetSubmitted}"), submitButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        buttonPanel.add(submitButton);

        retractButton.setAction(actionMap.get("retractHoliday")); // NOI18N
        retractButton.setText(resourceMap.getString("retractButton.text")); // NOI18N
        retractButton.setName("retractButton"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${retractable}"), retractButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        buttonPanel.add(retractButton);

        approveButton.setAction(actionMap.get("approveHoliday")); // NOI18N
        approveButton.setText(resourceMap.getString("approveButton.text")); // NOI18N
        approveButton.setName("approveButton"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${approvable}"), approveButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        buttonPanel.add(approveButton);

        rejectButton.setAction(actionMap.get("declineHoliday")); // NOI18N
        rejectButton.setText(resourceMap.getString("rejectButton.text")); // NOI18N
        rejectButton.setName("rejectButton"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${approvable}"), rejectButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        buttonPanel.add(rejectButton);

        unapproveButton.setAction(actionMap.get("unapproveHoliday")); // NOI18N
        unapproveButton.setText(resourceMap.getString("unapproveButton.text")); // NOI18N
        unapproveButton.setName("unapproveButton"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${unapprovable}"), unapproveButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        buttonPanel.add(unapproveButton);

        reportButton.setAction(actionMap.get("viewReport")); // NOI18N
        reportButton.setText(resourceMap.getString("reportButton.text")); // NOI18N
        reportButton.setName("reportButton"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${recordSelected}"), reportButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        buttonPanel.add(reportButton);

        getContentPane().add(buttonPanel);

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-806)/2, (screenSize.height-578)/2, 806, 578);
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        myParent.hideHolidayRequestDialog();
    }//GEN-LAST:event_formWindowClosing

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened

        // Enable/disable buttons based on logged in user

        setAdministratorLoggedIn(myParent.isSupervisorLoggedIn());
        approveButton.setVisible(isAdministratorLoggedIn());
        rejectButton.setVisible(isAdministratorLoggedIn());
        unapproveButton.setVisible(isAdministratorLoggedIn());

        // Apply a default filter
        
        setEmployeeFilterable(true);
        setFilteredEmployee(myParent.getLoggedInEmployee());

        setStatusFilterable(false);
        setFilteredStatus(null);

        dateFilterCombo.setSelectedIndex(THIS_YEARS_REQUESTS_ID);

        if (!Beans.isDesignTime()) {
            applyFilter();
        }
    }//GEN-LAST:event_formWindowOpened

    private void dateFilterComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_dateFilterComboItemStateChanged

        setDateComboBeingActioned(true);

        int selection = dateFilterCombo.getSelectedIndex();
        
        if (selection == ALL_REQUESTS_ID) {
            dateFilterFrom.setDate(DaisyChainCalendar.START_OF_TIME);
            dateFilterTo.setDate(DaisyChainCalendar.END_OF_TIME);

        } else if (selection == THIS_YEARS_REQUESTS_ID) {
            dateFilterFrom.setDate(DaisyChainCalendar.startOfYear());
            dateFilterTo.setDate(DaisyChainCalendar.endOfYear());

        } else if (selection == PAST_REQUESTS_ID) {
            dateFilterFrom.setDate(DaisyChainCalendar.START_OF_TIME);
            dateFilterTo.setDate(DaisyChainCalendar.endOfToday());
            
        } else if (selection == CURRENT_REQUESTS_ID) {
            dateFilterFrom.setDate(DaisyChainCalendar.startOfToday());
            dateFilterTo.setDate(DaisyChainCalendar.endOfToday());
            
        } else if (selection == FUTURE_REQUESTS_ID) {
            dateFilterFrom.setDate(DaisyChainCalendar.startOfToday());
            dateFilterTo.setDate(DaisyChainCalendar.END_OF_TIME);
        }

        setDateComboBeingActioned(false);
    }//GEN-LAST:event_dateFilterComboItemStateChanged

    private void dateFilterFromPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dateFilterFromPropertyChange
        if (!getDateComboBeingActioned()) {
            dateFilterCombo.setSelectedIndex(CUSTOM_REQUESTS_ID);
        }
    }//GEN-LAST:event_dateFilterFromPropertyChange

    private void dateFilterToPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dateFilterToPropertyChange
        if (!getDateComboBeingActioned()) {
            dateFilterCombo.setSelectedIndex(CUSTOM_REQUESTS_ID);
        }
    }//GEN-LAST:event_dateFilterToPropertyChange
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private TimeRecording.forms.AbsenceSummaryPanel absenceSummaryPanel1;
    private javax.swing.JTextField administrator;
    private javax.swing.JTextField administratorComment;
    private javax.swing.JLabel administratorCommentLabel;
    private javax.swing.JLabel administratorLabel;
    private javax.swing.JButton approveButton;
    private javax.swing.JPanel buttonPanel;
    private TimeRecording.widgets.DateConverter dateConverter;
    private javax.swing.JComboBox dateFilterCombo;
    private com.toedter.calendar.JDateChooser dateFilterFrom;
    private javax.swing.JLabel dateFilterFromLabel;
    private javax.swing.JLabel dateFilterLabel;
    private com.toedter.calendar.JDateChooser dateFilterTo;
    private javax.swing.JLabel dateFilterToLabel;
    private javax.swing.JButton deleteButton;
    private javax.swing.JPanel detailPanel;
    private javax.swing.JButton editButton;
    private javax.swing.JTextField employee;
    private javax.swing.JCheckBox employeeCheck;
    private javax.swing.JTextField employeeComment;
    private javax.swing.JLabel employeeCommentLabel;
    private javax.swing.JComboBox employeeFilterCombo;
    private javax.swing.JLabel employeeLabel;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JButton explanationButton;
    private javax.swing.JButton filterButton;
    private javax.swing.JPanel filterPanel;
    private javax.swing.JTextField firstDayPart;
    private javax.swing.JTextField fromDate;
    private javax.swing.JCheckBox fromFlexiCheck;
    private javax.swing.JLabel fromLabel;
    private java.util.List<TimeRecording.tables.HolidayRequest> holidayRequestList;
    private TimeRecording.enums.HolidayRequestStatusConverter holidayRequestStatusConverter;
    private TimeRecording.enums.HolidayRequestStatusConverterBeanInfo holidayRequestStatusConverterBeanInfo1;
    private javax.swing.JFormattedTextField hoursSpent;
    private javax.swing.JLabel hoursSpentLabel;
    private javax.swing.JTextField lastDayPart;
    private javax.swing.JButton newButton;
    private TimeRecording.enums.PartOfDayConverter partOfDayConverter;
    private javax.swing.JButton rejectButton;
    private javax.swing.JButton reportButton;
    private javax.swing.JTable requestTable;
    private javax.swing.JButton retractButton;
    private javax.swing.JTextField status;
    private javax.swing.JCheckBox statusCheck;
    private javax.swing.JComboBox statusFilterCombo;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JButton submitButton;
    private javax.swing.JScrollPane tablePane;
    private javax.swing.JTextField toDate;
    private javax.swing.JLabel toLabel;
    private javax.swing.JButton unapproveButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    @Action
    public void newHoliday() {

        HolidayRequest holiday = new HolidayRequest();
//        holida.setEmployeeId(myParent.getLoggedInEmployee());
        holiday.setEmployeeId((Employee)employeeFilterCombo.getSelectedItem());
        holiday.setStatus(HolidayRequestStatus.PREPARATION);

        HolidayRequestEditor editor = new HolidayRequestEditor(this);
        editor.setAdministratorLoggedIn(administratorLoggedIn);
        editor.setHoliday(holiday);
        editor.setVisible(true);
        if (!editor.isSaveSelected()) {
            return;
        }
        
        try {
            // Plumb in the correct user (one used in dialog probably stale)
            
            Employee selectedEmployee = entityManager.find(Employee.class,
                    holiday.getEmployeeId().getEmployeeId());

            // Ensure the holiday is plugged into the employee's details
            
            selectedEmployee.getHolidayRequests().add(holiday);

            holiday.setEmployeeId(selectedEmployee);

            entityManager.getTransaction().begin();
            entityManager.persist(holiday);

            // Reassess (and persist) holiday summary for this employee
            reassessAnnualSummary(holiday.getEmployeeId());
            
            entityManager.getTransaction().commit();
            entityManager.refresh(holiday.getEmployeeId());
            
            holidayRequestList.add(holiday);
            int row = holidayRequestList.size() - 1;
            requestTable.setRowSelectionInterval(row, row);
            requestTable.scrollRectToVisible(requestTable.getCellRect(row, 0, true));

        } catch (RuntimeException e) {
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
            e.printStackTrace();
            ErrorDialog.showError(this, e);
        }
        
        assessWhatShouldBeEnabled();
    }

    @Action
    public void editHoliday() {

        HolidayRequest holiday = holidayRequestList.get(
                requestTable.convertRowIndexToModel(
                        requestTable.getSelectedRow()));

        HolidayRequestEditor editor = new HolidayRequestEditor(this);
        editor.setAdministratorLoggedIn(administratorLoggedIn);
        editor.setHoliday(holiday);
        editor.setVisible(true);
        if (!editor.isSaveSelected()) {
            return;
        }

        try {
            entityManager.getTransaction().begin();
            entityManager.merge(holiday);

            // Reassess (and persist) holiday summary for this employee
            reassessAnnualSummary(holiday.getEmployeeId());
            
            entityManager.getTransaction().commit();

        } catch (RuntimeException e) {
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
            ErrorDialog.showError(this, e);
        }

        assessWhatShouldBeEnabled();
    }

    @Action
    public void deleteHoliday() {

        int choice = JOptionPane.showConfirmDialog(this,
                "Are you sure you wish to delete the request",
                "Delete Holiday Request", JOptionPane.YES_NO_OPTION);

        if (choice == JOptionPane.NO_OPTION) {
            return;
        }

        HolidayRequest holiday = holidayRequestList.get(
                requestTable.convertRowIndexToModel(
                        requestTable.getSelectedRow()));

        try {
            entityManager.getTransaction().begin();

            // Delete the actual holiday
            holiday.getEmployeeId().getHolidayRequests().remove(holiday);
            entityManager.remove(holiday);

            // Reassess (and persist) holiday summary for this employee
            reassessAnnualSummary(holiday.getEmployeeId());

            // Complete the transaction
            entityManager.getTransaction().commit();

            // Assume that no exceptions were thrown, remove the holiday from the GUI
            holidayRequestList.remove(holiday);

            entityManager.refresh(holiday.getEmployeeId());

        } catch (RuntimeException e) {
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
            ErrorDialog.showError(this, e);
        }

        assessWhatShouldBeEnabled();
    }

    private void changeStatus(HolidayRequestStatus status) {

        // TODO: Is this really the easiest way to do this?
        HolidayRequest holiday = holidayRequestList.get(
                requestTable.convertRowIndexToModel(
                        requestTable.getSelectedRow()));

        holiday.setStatus(status);

        if (status == HolidayRequestStatus.APPROVED ||
                status == HolidayRequestStatus.REJECTED) {
            holiday.setAdministratorId(myParent.getLoggedInEmployee());

        } else if (status == HolidayRequestStatus.SUBMITTED) {
            holiday.setAdministratorId(null);
        }

        try {
            entityManager.getTransaction().begin();
            entityManager.merge(holiday);

            // Reassess (and persist) holiday summary for this employee
            reassessAnnualSummary(holiday.getEmployeeId());

            entityManager.getTransaction().commit();

        } catch (RuntimeException e) {
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
            ErrorDialog.showError(this, e);
        }
        
        assessWhatShouldBeEnabled();
    }

    private void reassessAnnualSummary(Employee employee) {

        // Reassess annual holiday spend
        new HolidayCalculations().reassessSummary(entityManager, employee);

        // Repersist the annual holiday summary
        entityManager.merge(employee.getWorkingPatternsMaster());
    }

    @Action
    public void submitHoliday() {
        changeStatus(HolidayRequestStatus.SUBMITTED);
    }

    @Action
    public void approveHoliday() {
        changeStatus(HolidayRequestStatus.APPROVED);
    }

    @Action
    public void declineHoliday() {
        changeStatus(HolidayRequestStatus.REJECTED);
    }

    @Action
    public void unapproveHoliday() {
        changeStatus(HolidayRequestStatus.SUBMITTED);
    }

    @Action
    public void retractHoliday() {
        changeStatus(HolidayRequestStatus.PREPARATION);
    }

    @Action
    @SuppressWarnings("unchecked")
    public void applyFilter() {

        // Build a custom query with all required filters

        FilteredQuery filterQuery = new FilteredQuery(HolidayRequest.BASE_QUERY);

        if (isEmployeeFilterable() && filteredEmployee != null) {
            filterQuery.addClause(HolidayRequest.EMPLOYEE_FILTER);
        }

        if (isStatusFilterable() && filteredStatus != null) {
            filterQuery.addClause(HolidayRequest.STATUS_FILTER);
        }

        filterQuery.addClause(HolidayRequest.DATE_FILTER);

        Query query = entityManager.createQuery(filterQuery.getQuery());

        if (employeeFilterable && filteredEmployee != null) {
            query.setParameter(
                    HolidayRequest.COL_EMPLOYEE_ID,
                    filteredEmployee);
        }

        if (isStatusFilterable() && filteredStatus != null) {
            query.setParameter(
                    HolidayRequest.COL_STATUS,
                    filteredStatus);
        }

        query.setParameter(
                HolidayRequest.COL_LOWER_DATE,
                filteredStartDate);

        query.setParameter(
                HolidayRequest.COL_UPPER_DATE,
                filteredEndDate);

        holidayRequestList.clear();
        holidayRequestList.addAll(query.getResultList());
    }

    @Action
    public void explainClicked() {

        if (requestTable.getSelectedRow() == -1) {
            return;
        }

        HolidayRequest holiday = holidayRequestList.get(
                requestTable.convertRowIndexToModel(
                        requestTable.getSelectedRow()));

        if (holiday.getHoursSpent() != null &&
                holiday.getHoursSpent() > 0) {

            // Recalculate the holidays (with an expalantion)
            holidayCalculations.getHolidaySpend(
                    entityManager, holiday, explanation);

            // Dump the explanation to our dialog box
            InformationDialog.showInformation(this,
                    "Holiday Calculation", explanation.getExplanation());
        }
    }

    @Action
    public void viewReport() {

        Cursor savedCursor = getCursor();

        try {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

            HolidayRequest holiday = holidayRequestList.get(
                    requestTable.convertRowIndexToModel(
                            requestTable.getSelectedRow()));

            new CalendarReport().singleYearAbsenceReport(new DateRange(
                    DaisyChainUtilities.firstDayOfYear(null).getTime(),
                    DaisyChainUtilities.lastDayOfYear(null).getTime()),
                    new HolidayIconProvider(), holiday.getEmployeeId());
            
        } finally {
            setCursor(savedCursor);
        }
    }
}